﻿Imports Crystal.CommonDBManagement

Public Class frmFundUploadValidations
    Private _dtValidationTypes As DataTable
    Private _DuplicateRecord As DataTable
    Public Event ReRunEvent()

#Region "Constructors"
    'Constructor Getting the ExcelData
    Sub New(ByVal ldtValidationTypes As DataTable, ByVal DuplicateRecord As DataTable)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        _dtValidationTypes = ldtValidationTypes
        _DuplicateRecord = DuplicateRecord
    End Sub
#End Region

#Region "OnLoad"

    Private Sub frmInvestorUploadValidations_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        grdValidationType.DataSource = _dtValidationTypes
        grdNotImported.DataSource = Nothing
        grdNotImported.DataSource = _DuplicateRecord
        lblMessage.Visible = False
        'grdNotImported.DataSource = _DuplicateRecord

        grdValidationType.DisplayLayout.Bands(0).Columns("Message").Hidden = True
        grdNotImported.DisplayLayout.Bands(0).Columns("Message").Hidden = True
        grdNotImported.Rows.ColumnFilters("Message").FilterConditions.Add(FilterComparisionOperator.Equals, grdValidationType.Rows(0).Cells("Message").Value)
        Me.grdValidationType.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        Dim value As String = String.Empty
        For Each row As DataRow In _dtValidationTypes.Rows
            value = row("Message").ToString()
            If value = BLLmdlCommon.GridRecord.CountryNotExists Or value = BLLmdlCommon.GridRecord.CityNotExist _
               Or value = BLLmdlCommon.GridRecord.StateNotExists Or value = BLLmdlCommon.GridRecord.TypeNotExistsInMDB Then
                lblMessage.Visible = True
                Exit For
            End If
        Next
    End Sub

#End Region

    Private Sub grdValdationType_AfterRowActivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdValidationType.AfterRowActivate
        'Dim filtercondition As New Infragistics.Win.UltraWinGrid.FilterCondition
        'filtercondition.ComparisionOperator = FilterComparisionOperator.Equals
        If grdValidationType.ActiveRow IsNot Nothing Then
            grdNotImported.Rows.ColumnFilters("Message").FilterConditions(0).CompareValue = grdValidationType.ActiveRow.Cells("Message").Value
            grdNotImported.DisplayLayout.RefreshFilters()
        End If
        Me.grdValidationType.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
    End Sub

    Private Sub grdValidationType_BeforeEnterEditMode(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdValidationType.BeforeEnterEditMode
        grdValidationType.ActiveRow.Activation = Activation.ActivateOnly
    End Sub

    Private Sub btnReRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReRun.Click
        RaiseEvent ReRunEvent()
        Me.Close()
    End Sub

    Private Function IsChildFormOpened() As Boolean
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "frmUploadFund" Then
                Application.OpenForms(lmFomrs).BringToFront()
                'Return False
            End If
        Next
        Return True
    End Function

    Private Sub grdValidationType_DoubleClickRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs) Handles grdValidationType.DoubleClickRow
        Dim listNewType As New List(Of String)
        Dim Text As String = String.Empty
        Dim strPara As String = String.Empty
        Dim drow As DataRow
        Dim lkpTable As New DataTable
        Dim addEditLKPTable As New frmAddEditLKPTable
        Me.grdValidationType.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        'If (CType(sender, Control).Name = "btnAddInvestor") Then
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "frmAddEditLKPTable" Then
                If (FocusTheExistingInstanceOfForm(0, "frmAddEditLKPTable", CType(Application.OpenForms(lmFomrs), frmAddEditLKPTable).WindowState)) Then
                    Return
                End If
            End If
        Next
        'End If

        Try
            Select Case grdValidationType.ActiveRow.Cells("Message").Value.ToString
                

                Case BLLmdlCommon.GridRecord.StyleNotExists
                    addEditLKPTable.SelectValue = LKPRefTable.Style
                    addEditLKPTable.CheckValidationForm = True
                    lkpTable.Columns.Add("Style", Type.GetType("System.String"))
                    For Each value As UltraGridRow In grdNotImported.Rows
                        If value.Cells("Message").Value = grdValidationType.ActiveRow.Cells("Message").Value Then
                            If value.Cells("Style").Value.ToString <> String.Empty Then
                                If listNewType.Contains(value.Cells("Style").Value.ToString) = False Then
                                    listNewType.Add(value.Cells("Style").Value.ToString())
                                    drow = lkpTable.NewRow()
                                    drow(0) = value.Cells("Style").Value.ToString
                                    'drow(1) = value.Cells("MasterInvestorTypeID").Value.ToString
                                    lkpTable.Rows.Add(drow)
                                End If
                            End If
                        End If
                    Next
                    addEditLKPTable.ValidatedData = lkpTable
                    addEditLKPTable.NewRecords = listNewType
                    addEditLKPTable.Show()

                Case BLLmdlCommon.GridRecord.TurnoverNotExistsInDB
                    addEditLKPTable.SelectValue = LKPRefTable.Turnover
                    addEditLKPTable.CheckValidationForm = True
                    lkpTable.Columns.Add("Turnover", Type.GetType("System.String"))
                    For Each value As UltraGridRow In grdNotImported.Rows
                        If value.Cells("Message").Value = grdValidationType.ActiveRow.Cells("Message").Value Then
                            If value.Cells("Turnover").Value.ToString <> String.Empty Then
                                If listNewType.Contains(value.Cells("Turnover").Value.ToString) = False Then
                                    listNewType.Add(value.Cells("Turnover").Value.ToString())
                                    drow = lkpTable.NewRow()
                                    drow(0) = value.Cells("Turnover").Value.ToString
                                    'drow(1) = value.Cells("MasterInvestorTypeID").Value.ToString
                                    lkpTable.Rows.Add(drow)
                                End If
                            End If
                        End If
                    Next
                    addEditLKPTable.ValidatedData = lkpTable
                    addEditLKPTable.NewRecords = listNewType
                    addEditLKPTable.Show()

                Case BLLmdlCommon.GridRecord.InvalidFundType
                    addEditLKPTable.SelectValue = LKPRefTable.FundType
                    addEditLKPTable.CheckValidationForm = True
                    lkpTable.Columns.Add("FundType", Type.GetType("System.String"))
                    For Each value As UltraGridRow In grdNotImported.Rows
                        If value.Cells("Message").Value = grdValidationType.ActiveRow.Cells("Message").Value Then
                            If value.Cells("Type").Value.ToString <> String.Empty Then
                                If listNewType.Contains(value.Cells("Type").Value.ToString) = False Then
                                    listNewType.Add(value.Cells("Type").Value.ToString())
                                    drow = lkpTable.NewRow()
                                    drow(0) = value.Cells("Type").Value.ToString
                                    'drow(1) = value.Cells("MasterInvestorTypeID").Value.ToString
                                    lkpTable.Rows.Add(drow)
                                End If
                            End If
                        End If
                    Next
                    addEditLKPTable.ValidatedData = lkpTable
                    addEditLKPTable.NewRecords = listNewType
                    addEditLKPTable.Show()

                Case BLLmdlCommon.GridRecord.CountryNotExists
                    addEditLKPTable.SelectValue = LKPRefTable.Country
                    addEditLKPTable.CheckValidationForm = True
                    lkpTable.Columns.Add("CountryCode", Type.GetType("System.String"))
                    For Each value As UltraGridRow In grdNotImported.Rows
                        If value.Cells("Message").Value = grdValidationType.ActiveRow.Cells("Message").Value Then
                            If listNewType.Contains(value.Cells("Country").Value.ToString) = False Then
                                listNewType.Add(value.Cells("Country").Value.ToString())
                                drow = lkpTable.NewRow()
                                drow(0) = value.Cells("Country").Value.ToString
                                lkpTable.Rows.Add(drow)
                            End If
                        End If
                    Next
                    addEditLKPTable.ValidatedData = lkpTable
                    addEditLKPTable.NewRecords = listNewType
                    addEditLKPTable.Show()
            End Select
        Catch ex As Exception

        End Try
    End Sub
End Class